/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.SearchSupplier;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pojo.Provider;

/**
 *
 * @author A7med Kotb
 */
public class AjaxSearchAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String param=request.getParameter("param");
        if(param != "0")
        {
            String level=request.getParameter("level");
            int acclevel=Integer.parseInt(level);
            SearchSupplier ss=new SearchSupplier();
            //Vector<Provider> myvect=ss.searchProvider(param,acclevel); 
            StringBuffer result=new StringBuffer();
            result.append(" <table border=\"1\">  <tr>  <th> Provider Name </th> <th> Type </th> <th> Headers </th> <th> Table Name </th></tr>");
            Vector<Provider> pactivities= ss.searchProvider(getDataSource(request,"MyDS").getConnection(),param,acclevel);
            for (int i = 0; i < pactivities.size(); i++) 
            {
                result.append("<tr> <td> "+pactivities.get(i).getName()+" </td>   <td> "+ pactivities.get(i).getType()+" </td> "
                    + " <td>"+pactivities.get(i).getHeaders()+" </td> <td>"+pactivities.get(i).getTableName()+" </td></tr>");
            }
            result.append("</table>");
            response.getWriter().println(result.toString());
        }
        else     
            response.getWriter().println("No Data Found");
        return null;
    }
}
